[Previous] [Next]

The Slider Control

The Slider control provides a way for end users to select a numerical value in a range. Conceptually, it's akin to the ScrollBar control, with which it shares many properties and events. A major difference is that there's only one kind of Slider control, which can create both horizontal and vertical sliders. The Slider control can also work in select-range mode, allowing your users to select a range rather than a single value.

Setting Design-Time Properties

Once you drop a Slider control on a form, you should right-click it and select the Properties menu command. In the General tab of the Properties custom dialog box, you can set the Min, Max, SmallChange, and LargeChange properties, which have the same meaning and effects as in HScrollBar and VScrollBar controls. In this tab, you can also set the SelectRange property, but this operation is most often performed at run time. (See "Employing the SelectRange Mode" later in this section.)

In the Appearance tab, you set a few properties that are peculiar to this control. The Orientation property lets you set the direction of the slider. The TickStyle property lets you select whether the slider has unit ticks and where they appear. (Valid values are 0-sldBottomRight, 1-sldTopLeft, 2-sldBoth, and 3-sldNoTicks.) The TickFrequency property indirectly determines how many ticks will be displayed. For example, if Min is 0 and Max is 10 (the default settings), a TickFrequency that equals 2 displays 6 ticks. The TextPosition property lets you decide where the ToolTip appears. (See "Showing the Value as a ToolTip" later in this section.)

Run-Time Operations

For most practical purposes, you can deal with a Slider control at run time as if it were a scroll bar control: Slider controls expose the Value property and the Change and Scroll events, exactly as scroll bars do. The following brief sections describe two features of the Slider control that are missing from the scroll bar controls.

Showing the value as a ToolTip

Slider controls can display ToolTip text that follows the indicator when it's being dragged by the user. You can control this new Visual Basic 6 feature using two properties, Text and TextPosition. The former is the string that appears in the ToolTip window; the latter determines where the ToolTip appears with respect to the indicator. (Possible values are 0-sldAboveLeft and 1-sldBelowRight.) You can also set the TextPosition property at design time in the Appearance tab of the Property Pages dialog box.

You generally use these properties to show the current value in a ToolTip window near the indicator. To do so, you need just one statement in the Scroll event procedure:

Private Sub Slider1_Scroll()
    Slider1.Text = "Value = " & Slider1.Value
End Sub

Employing the SelectRange mode

The Slider control supports the ability to display a range instead of an individual value, as you can see in Figure 10-25. To display a range, you use a number of properties together. First of all, you enter select range mode by setting the SelectRange property to True—for example, when users click on the control while they're pressing the Shift key.

Dim StartSelection As Single 

Private Sub Slider1_MouseDown(Button As Integer, Shift As Integer, _
    x As Single, y As Single)
    If Shift = vbShiftMask Then
        ' If the shift key is being pressed, enter select range mode.
        Slider1.SelectRange = True
        Slider1.SelLength = 0
        StartSelection = Slider1.Value
    Else
        ' Else cancel any active select range mode.
        Slider1.SelectRange = False
    End If
End Sub

Click to view at full size.

Figure 10-25. You can use a Slider control to select a range, and you can also display a ToolTip beside the indicator being dragged.

After you enter the select range mode, you can control the interval being highlighted by means of the SelStart and SelLength properties. You do this in the Scroll event procedure. Because SelLength can't be negative, you must account for two distinct cases:

Private Sub Slider1_Scroll()
    If Slider1.SelectRange Then
        ' The indicator is being moved in SelectRange mode.
        If Slider1.Value > StartSelection Then
            Slider1.SelStart = StartSelection
            Slider1.SelLength = Slider1.Value - StartSelection
        Else
            Slider1.SelStart = Slider1.Value
            Slider1.SelLength = StartSelection - Slider1.Value
        End If
    End If
End Sub